package com.astn.adapter.booking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.astn.bks.R;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.components.ActionItem;
import com.astn.entity.booking.COfferHolder;
import com.astn.entity.booking.NameValueHolder;
import com.astn.entity.booking.QuoteOffer;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class COfferAdapter extends BaseAdapter {
	// private ArrayList<QuoteOffer> lstItems = new ArrayList<QuoteOffer>();
	private JsonArray jarItems = new JsonArray();
	private Context context;
	LayoutInflater oInflater;

	public COfferAdapter() {

	}

	// public COfferAdapter(Context context, ArrayList<QuoteOffer> lstItems) {
	// this.context = context;
	// this.lstItems = lstItems;
	// }
	public COfferAdapter(Context context, JsonArray jarItems) {
		this.context = context;
		this.jarItems = jarItems;
		oInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jarItems == null ? 1 : jarItems.size() + 1;
	}

	@Override
	public JsonObject getItem(int arg0) {
		// TODO Auto-generated method stub

		return arg0 <= 0 ? null : jarItems.get(arg0 - 1).getAsJsonObject();
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		COfferHolder holder;
		if (convertView == null) {
			if (position > 0) {
				convertView = oInflater.inflate(R.layout.cofferitem, null);
				holder = new COfferHolder();
				holder.txvProvider = (TextView) convertView
						.findViewById(R.id.txvProvider);
				holder.txvPrice = (TextView) convertView
						.findViewById(R.id.txvPrice);
			} else {
				convertView = oInflater.inflate(R.layout.cofferitem1item, null);
				holder = new COfferHolder();
			}
			// holder.status.
			convertView.setTag(holder);
		} else {
			holder = (COfferHolder) convertView.getTag();
		}
		if (position > 0) {
			try {
				JsonObject jsonObj = jarItems.get(position - 1)
						.getAsJsonObject().get("fromUser").getAsJsonObject();
				holder.txvProvider.setText(jsonObj.get("name").getAsString());
				holder.txvPrice.setText(jarItems.get(position - 1)
						.getAsJsonObject().get("price").getAsString());
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		// holder.txvInfo.setText(lstItems.get(position).getCountryid());
		return convertView;

	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return 0;
		}
		return 1;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}
}
